package com.example.jk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@Controller
public class Controllerr {

    static int width=2970;
    static int height=2100;
    /**
     *     线条两边的间距
     */
    static int lineMargin=200;

    /**
     *    正文左间距
     */
    static int textMargin = lineMargin + 100;
    static double topMargin = 0.05;
    /**
     * 标题
     */
    static Font font = new Font("微软雅黑", Font.BOLD, 50);
    /**
     * 正文
     */
    static Font fontText = new Font("微软雅黑", Font.PLAIN, 40);
    String prescriptionTitle = "标题";
    String str1 = "文本1111";


    @GetMapping("/get")
    @ResponseBody
    public String get(){
        return "OKOKOKOKOKOK";
    }


    @GetMapping("/huiTu")
    @ResponseBody
    public void huiTu(){
        //创建图片对象
        BufferedImage image = new BufferedImage(width, height,  BufferedImage.TYPE_INT_RGB);
        //基于图片对象打开绘图
        Graphics2D g2 = image.createGraphics();

        //设置颜色
        g2.setColor(Color.white);
        //设置背景
        g2.fillRect(0, 0, width, height);
        float d = 4;
        g2.setStroke(new BasicStroke(d));


        //设置字体颜色
        g2.setColor(Color.BLACK);
        //设置字体:字体、字号、大小
        g2.setFont(font);

        //标题
        FontMetrics fontMetrics = g2.getFontMetrics(font);

        // 计算出中心点 x 位置
        int centerX = width / 2;

        // 文字宽度-标题
        int textWidth = fontMetrics.stringWidth(prescriptionTitle);
        g2.drawString(prescriptionTitle, centerX - textWidth / 2, (int) (height * 0.08));
        // 文字宽度
        int textWidth1 = fontMetrics.stringWidth(str1);
        g2.drawString(str1, centerX - textWidth1 / 2, (int) (height * 0.12));

        //右上方：正方形（X坐标，Y坐标，宽，高）
        Rectangle2D rectangle2D = new Rectangle2D.Float(width / 4 * 3, (int) (height * 0.04), 200, 200);
        g2.draw(rectangle2D);

        //正方形内的文字
        g2.drawString("正方形内的文字", width / 4 * 3 + 10, (int) (height * 0.08));

        //设置字体:字体、字号、大小
        g2.setFont(fontText);
        //画线条：2 基数 0.17
        double double2 = 0.17;
        Line2D line2D1 = new Line2D.Float(lineMargin, (int) (height * double2), width - lineMargin, (int) (height * double2));
        //画线条
        g2.draw(line2D1);

        //画线条 上间距+5
        Line2D line2D2 = new Line2D.Float(lineMargin, (int) (height * (double2 + topMargin * 3)), width - lineMargin, (int) (height * (double2 + topMargin * 3)));
        g2.draw(line2D2);


        File f = new File("C:\\Users\\ZSG\\Desktop");
        // 判断文件父目录是否存在
        if (!f.exists()) {
            f.mkdirs();
        }

        //保存图片 JPEG表示保存格式
        try {
            ImageIO.write(image, "png", new File(f + "\\" + "aa.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
